using System.Collections.Generic;
using System.Collections.Specialized;

namespace KlearTweet.Client.Model
{
    public class EntityCollection : List<IEntity>, INotifyCollectionChanged
    {
        public event NotifyCollectionChangedEventHandler CollectionChanged;

        public void RaiseCollectionChanged(bool reset)
        {
            var handler = CollectionChanged;
            if (handler != null)
            {
                var args = reset ? new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)
                    : new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, null, null, 0);
                handler(this, args);
            }
        }

        public new void Sort()
        {
            Sort(EntityComparer);
        }

        private static int EntityComparer(IEntity e1, IEntity e2)
        {
            if (e1 is Account && e2 is Account)
                return ((Account)e1).User.UserName.CompareTo(((Account)e2).User.UserName);

            if (e1 is User && e2 is User)
                return ((User)e1).UserName.CompareTo(((User)e2).UserName);

            if (e1 is Tweet && e2 is Tweet)
                return ((Tweet)e2).CreatedDate.CompareTo(((Tweet)e1).CreatedDate);

            throw new System.Exception("Comparison not suppported: e1=" + e1 + ", e2=" + e2);
        }
    }
}